<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/11/0011
 * Time: 14:02
 */

namespace app\sports_admin\model;


use think\Db;
use think\Model;

class WatchInstructionsRecord extends Model
{
    protected $autoWriteTimestamp = true;

    protected $pk = 'id';

    protected $table = 'watch_instructions_record';

    /*
     * 获取步数信息
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     * @param $start 开始时间
     * @param $end 结束时间
     */
    public static function getWatchInstructionsRecordInfo($page,$rows,$sort,$sortOrder,$searchName,$start,$end)
    {
        $result = self::where(function($query)use($searchName,$start,$end){
            if($searchName){
                $query->where('number','like',"%{$searchName}%");
            }
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}","{$end}"]);
            }
        })->order("{$sort}","{$sortOrder}")->limit($rows)->page($page)->select();
        $count = self::where(function($query)use($searchName,$start,$end){
            if($searchName){
                $query->where('number','like',"%{$searchName}%");
            }
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}","{$end}"]);
            }
        })->count();
        return json(['rows'=>$result,'total'=>$count]);
    }
    /*
    * 获取单个设备指令下发记录
    * @param $page 第几页
    * @param $rows 每页显示的条数
    * @param $sort 要排序的字段
    * @param $sortOrder 升序/降序
    * @param $number 设备编号
    * @param $start 开始时间
    * @param $end 结束时间
    */
    public static function communicationList($page,$rows,$sort,$sortOrder,$number,$start,$end)
    {
        $result = self::where(function($query)use($start,$end){
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}","{$end}"]);
            }
        })->where('number',$number)->order("{$sort}","{$sortOrder}")->limit($rows)->page($page)->select();
//        var_dump(Db::getLastSql());
//        return;
        $total = self::where(function($query)use($start,$end){
            if($start && $end){
                $query->whereTime('create_time','between',["{$start}","{$end}"]);
            }
        })->where('number',$number)->count();
        return json(['rows'=>$result,'total'=>$total]);
    }
}